package com.smart.translator.data;

import android.util.Log;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture;

/**
 * 配置API服务类
 */
public class ConfigApiService {
    private static final String TAG = "ConfigApiService";

     //private static final String CONFIG_URL = "http://qixin.yeshan.fun/v1/app-config";
    private static final String CONFIG_URL = "http://112.74.97.118/v1/app-config";

    /**
     * 获取应用配置
     */
    public static CompletableFuture<ApiResponse<AppConfig>> getAppConfig() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                // 发送GET请求
                HttpURLConnection connection = (HttpURLConnection) new URL(CONFIG_URL).openConnection();
                connection.setRequestMethod("GET");
                connection.setRequestProperty("Content-Type", "application/json");

                // 读取响应
                int responseCode = connection.getResponseCode();
                StringBuilder response = new StringBuilder();
                
                try (BufferedReader br = new BufferedReader(
                        new InputStreamReader(responseCode >= 400 ? connection.getErrorStream() : connection.getInputStream(), StandardCharsets.UTF_8))) {
                    String line;
                    while ((line = br.readLine()) != null) {
                        response.append(line);
                    }
                }

                Log.d(TAG, "配置响应: " + response.toString());

                if (responseCode == 200) {
                    // 请求成功
                    JSONObject jsonResponse = new JSONObject(response.toString());
                    boolean success = jsonResponse.optBoolean("success", false);
                    
                    if (success) {
                        JSONObject data = jsonResponse.optJSONObject("data");
                        if (data != null) {
                            AppConfig config = new AppConfig();
                            config.setTtsProvider(data.optString("tts_provider", ""));
                            config.setAsrProvider(data.optString("asr_provider", ""));
                            config.setServerUrl(data.optString("server_url", ""));
                            config.setAppVersion(data.optString("app_version", ""));
                            config.setCm3Version(data.optString("cm3_version", ""));
                            config.setFlashVersion(data.optString("flash_version", ""));
                            
                            Log.d(TAG, "配置获取成功: " + config.toString());
                            return ApiResponse.success(config);
                        } else {
                            return ApiResponse.error("配置数据为空");
                        }
                    } else {
                        String message = jsonResponse.optString("message", "获取配置失败");
                        return ApiResponse.error(message);
                    }
                } else {
                    // 请求失败
                    return ApiResponse.error("HTTP错误: " + responseCode);
                }

            } catch (Exception e) {
                Log.e(TAG, "获取配置失败", e);
                return ApiResponse.error("网络请求失败: " + e.getMessage());
            }
        });
    }
} 